package Z.dailyExercise.May;

import java.util.Stack;

public class _739每日温度 {

    /*
    暴力解
     */
    public int[] dailyTemperatures(int[] T) {
        int[] res = new int[T.length];
        for (int i = 0; i < T.length; i++) {
            for (int j = i+1; j < T.length; j++) {
                if (T[j]>T[i]){
                    res[i] = j-i;
                    break;
                }

            }

        }
        return res;

    }
    /**
     * 题解：建立一个单调递减栈 存储元素的下标  被弹出的进行结果的输出
     *
     */
    public int[] dailyTemperatures1(int[] T) {
        Stack<Integer> stack = new Stack<>();
        int[] res = new int[T.length];
        for (int i = 0; i < T.length; i++) {
            while (!stack.empty() && T[stack.peek()]<T[i]){
                int index = stack.pop();
                res[index] = i-index;
            }
            stack.push(i);
        }

        return res;
    }
}
